package norman.studyproject.springboot;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.util.Date;

@RestController
public class HelloController {
    @Value("${name}")
    private String sixsixsix;
    @RequestMapping(value = "/hello",method = RequestMethod.POST)
    public String hello(@RequestParam("age")String arg1) {
        return "hello fresh guy" + sixsixsix+"age:"+arg1;
    }
    @RequestMapping(value = "pics",method = RequestMethod.GET)
    public ResponseEntity<FileSystemResource> exportpic()
    {
            return export(new File("C:\\test.jpg"));
    }
    public ResponseEntity<FileSystemResource> export(File file){
        HttpHeaders headers = new HttpHeaders();
        headers.add("Cache-Control", "no-cache, no-store, must-revalidate");
        headers.add("Content-Disposition", "attachment; filename="+file.getName());
        headers.add("Pragma", "no-cache");
        headers.add("Expires", "0");
        headers.add("Last-Modified", new Date().toString());
        headers.add("ETag", String.valueOf(System.currentTimeMillis()));

        return ResponseEntity
                .ok()
                .headers(headers)
                .contentLength(file.length())
                .contentType(MediaType.parseMediaType("application/octet-stream"))
                .body(new FileSystemResource(file));
    }
}

